<?php
session_start();
include_once("../util/constant.php");
/*
upload mode:
0: product
1: special product
2: product status
3: discount
4: article
5: banner
6: new product banner
*/
$path = "";
$maxNumOfFiles = null;
$isUsedSessionId = false; // only use session id when creating objects

if(isset($_SESSION["uploadMode"])){
	if(isset($_SESSION["newAction"])) {
		$path = "tmp/";
		$isUsedSessionId = true;
	}
	
	switch ($_SESSION["uploadMode"]) {
		case UPLOAD_MODE_PRODUCT:
			setPathAndMaxNumOfFiles($path, "product".(isset($_SESSION["proId"]) ? "/".$_SESSION["proId"] : ""),
                                    $maxNumOfFiles, null); 
			break;
		case UPLOAD_MODE_SPECIAL_PRODUCT:
            setPathAndMaxNumOfFiles($path, "product/special".(isset($_SESSION["speProId"]) ? "/".$_SESSION["speProId"] : ""),
                                    $maxNumOfFiles, null);
			break;
		case UPLOAD_MODE_PRODUCT_STATUS:
            setPathAndMaxNumOfFiles($path, "product-status".(isset($_SESSION["proStatId"]) ? "/".$_SESSION["proStatId"] : ""),
                                    $maxNumOfFiles, 1);
			break;
		case UPLOAD_MODE_DISCOUNT:
            setPathAndMaxNumOfFiles($path, "discount".(isset($_SESSION["disId"]) ? "/".$_SESSION["disId"] : ""),
                                    $maxNumOfFiles, 1); 
			break;
        case UPLOAD_MODE_ARTICLE:
            setPathAndMaxNumOfFiles($path, "article".(isset($_SESSION['article_id']) ? "/".$_SESSION['article_id'] : ""),
                                    $maxNumOfFiles, null); 
			break;
        case UPLOAD_MODE_BANNER:
            setPathAndMaxNumOfFiles($path, "banner".(isset($_SESSION['banner_id']) ? "/".$_SESSION['banner_id'] : ""),
                                    $maxNumOfFiles, 1); 
			break;
		case UPLOAD_MODE_NEW_PRODUCT_BANNER:
            setPathAndMaxNumOfFiles($path, "banner/new-product-banner".(isset($_SESSION["newProductBannerId"]) ? "/".$_SESSION["newProductBannerId"] : ""),
                                    $maxNumOfFiles, 1); 
			break;
	}
}

function setPathAndMaxNumOfFiles(&$uploadPath, $filePath, &$maxNumOfFiles, $num){
	$uploadPath .= $filePath;
    $maxNumOfFiles = $num;
}

//error_reporting(E_ALL | E_STRICT);
require('../util/UploadHandler.php');
$upload_handler = new UploadHandler(array(
	'user_dirs' => $isUsedSessionId,
	'upload_dir' => $path.'/',
	'upload_url' => UploadHandler::get_full_url().'/'.$path.'/',
	'max_number_of_files' => $maxNumOfFiles,
	'thumbnail' => array(
		'upload_dir' => $path.'/thumbnail/',
		'upload_url' => UploadHandler::get_full_url().'/'.$path.'/thumbnail/',
		'max_width' => ($_SESSION["uploadMode"] == UPLOAD_MODE_NEW_PRODUCT_BANNER) ? 506 : 350,
		'max_height' => ($_SESSION["uploadMode"] == UPLOAD_MODE_NEW_PRODUCT_BANNER) ? 506 : 600
		)
    ));

?>
